<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var obj = {
            userscore: [
                {
                    username: "老王",
                    score: 60
                },
                {
                    username: "老李",
                    score: 70
                },
                {
                    username: "老高",
                    score: 80
                },
                {
                    username: "老洪",
                    score: 90
                }
            ],
        };

        Object.defineProperties(obj, {
            sum: {
                get: function () {
                    // 获取userscore属性
                    let arr = this.userscore;
                    // 设置sum初始值
                    let add = 0;
                    for (var key of arr) {
                        add += key.score
                    }
                    return add;
                }
            },
            arg: {
                get: function () {
                    return this.sum / (this.userscore.length)
                },
            }
        })
        console.log(obj.sum);
        console.log(obj.arg);



        obj.userscore.push({
            username: '老张',
            score: 100
        });

        console.log(obj.sum);
        console.log(obj.arg);


/* ----------------------------------------------------------------------------------------------------------- */

        /* 
            数组新的循环方法
            reduce(function(pre,item){
                return pre
            },Initial )
            // 第一个参数 是 函数
            // 函数上的2个参数
            // 1. pre 上一次循环所得到的返回值
            // 2. item 当前循环数据
            // 第二个参数 是 初始值,第一次循环的pre值
        */
        var obj1 = {
            userscore: [
                {
                    username: "老王",
                    score: 60
                },
                {
                    username: "老李",
                    score: 70
                },
                {
                    username: "老高",
                    score: 80
                },
                {
                    username: "老洪",
                    score: 90
                }
            ],
        };

        Object.defineProperties(obj1, {
            sum: {
                get: function () {
                    // 获取userscore属性
                    let arr = this.userscore;
                    return arr.reduce(function (pre, item) {
                        return pre + item.score;
                    }, 0)
                }
            },
            arg: {
                get: function () {
                    return this.sum / (this.userscore.length)
                },
            }
        })
        console.log(obj1.sum);
        console.log(obj1.arg);



        obj1.userscore.push({
            username: '老张',
            score: 100
        });

        console.log(obj1.sum);
        console.log(obj1.arg);
    </script>
</body>

</html>